const sql = require('../sql/ip.js'); //导入sql
const db = require('../config/db.js'); //导入数据库配置

class Ip {
	constructor() {
		this.id = null; //门店编号
		this.store_name = ''; //门店名称
		this.store_address = ''; //门店地址
		this.latitude = ''; //纬度
		this.longitude = ''; //经度

	}
	/**
	 * @param {Ip} ip
	 */
	set(ip) {
		this.store_name = ip.store_name; //门店名称
		this.store_address = ip.store_address; //门店地址
		this.latitude = ip.latitude; //纬度
		this.longitude = ip.longitude; //经度
	}
	get() {
		return {
			id: this.id,
			store_name: this.store_name,
			store_address: this.store_address,
			latitude: this.latitude,
			longitude: this.longitude
		}
	}
	/**
	 * 获取附近门店
	 * @param {Ip} ip
	 * @param {string | number} range
	 */
	async selectStoresList(ip,range) {
		try {
			const ps = [ip.latitude, ip.longitude,range];
			const rows = await db.query(sql.selectStoresList, ps);
			return rows[0];
		} catch (error) {
			throw new Error(error.message);
		}
	}
	/**
	 * 根据经纬度查询指定门店
	 * @param {Ip} ip
	 */
	async selectStores(ip) {
		try {
			const ps = [ip.latitude, ip.longitude];
			const rows = await db.query(sql.selectStores, ps);
			return rows[0];
		} catch (error) {
			throw new Error(error.message);
		}
	}
}

module.exports = Ip;